package com.uchengwang.ui.cm_ui.rychat.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.adapter.BaseCycleAdapter;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.BaseViewHolder;
import com.uchengwang.ui.cm_ui.me.ui.GroupInfoActivity;
import com.uchengwang.ui.cm_ui.me.ui.MoreMemebersActivity;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;


/**
 * Created by U城网 on 2017/10/22.
 */

public class RyGroupMemberAdapter extends BaseCycleAdapter<RyUserBean, RecyclerView.ViewHolder>{
    private Context context;
    private int type;//9查看群成员 8管理权转让 7移除成员
    private long owenId, groupId;

    public RyGroupMemberAdapter(Context context, long owenId, long groupId, int type){
        this.context = context;
        this.owenId = owenId;
        this.groupId = groupId;
        this.type = type;
    }

    public void setType(int type){
        this.type = type;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_view, parent, false);
        return new BaseViewHolder(view , type);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        BaseViewHolder holder = (BaseViewHolder) h;

        final RyUserBean userBean = list.get(position);
        if (!TextUtils.isEmpty(userBean.getMemberAvatar())){
            ImageManager.getInstance().loadImage(context, holder.head, userBean.getMemberAvatar(), ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }
        String name = userBean.getRemarkName();
        if (!TextUtils.isEmpty(name)){
            holder.name.setText(name);
        }else {
            holder.name.setText(userBean.getMemberNickName());
        }
        String desc = userBean.getMemberNickName();
        holder.des.setText(TextUtils.isEmpty(desc) ? "暂无个性标语" : desc);

        //在社团成员列表里
        if (owenId >0){
            if (userBean.getMemberUid() == owenId){
                //当前用户是群主
                holder.apply_type.setVisibility(View.VISIBLE);
                holder.apply_type.setText("群主");
            }else {
                holder.apply_type.setVisibility(View.GONE);
            }
        }

        switch (type){
            case 7://移除好友
                if (owenId == userBean.getMemberUid()){//是群主
                    holder.apply_del.setVisibility(View.INVISIBLE);
                }else {
                    holder.apply_del.setVisibility(View.VISIBLE);
                }
                break;
        }

        holder.apply_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                userBean.setCheck(b);
            }
        });

        holder.apply_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //移除群成员
                removeMember(userBean);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 9://查看群成员
                        UCommonUtil.getUtil().toUserActivity((Activity) context, userBean.getMemberUid());
                        break;
                    case 8://1管理权转让
                        if (owenId ==userBean.getMemberUid()){
                            UCommonUtil.getUtil().showToast(context, "自己已是群组");
                        }else {
                            exChange(userBean.getMemberUid(), userBean.getMemberNickName());
                        }
                        break;
                    case 7://移除群成员
                        if (owenId != userBean.getMemberUid()){//不是群主
                            removeMember(userBean);
                        }
                        break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 管理权转让
     * */
    private AlertDialog dialog;
    private void exChange(final long newOwnerId, String nickName){
        dialog = DialogUtils.getUtils().showDialogAll(context, false, "", "确定将群主权限移交给" + nickName + "?", "取消", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                //移交管理权
                RyGroupManager.getManager().tranferGroup((Activity) context, groupId, newOwnerId, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas != 0){
                            UCommonUtil.getUtil().showToast(context, msg);
                            return;
                        }
                        UCommonUtil.getUtil().showToast(context, "更改成功");
                        GroupInfoActivity.isDeleteMember = true;
                        ((MoreMemebersActivity)context).finish();
                    }
                });
            }
        }, null);
    }

    /**
     * 移除群成员
     * */
    private void removeMember(final RyUserBean bean){
        dialog = DialogUtils.getUtils().showDialogAll(context, false, "", "确定将" + bean.getMemberNickName() + "移除本群?", "取消", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                LoadingUtils.getUtils().loading(context);
                RyGroupManager.getManager().exitGroup((Activity) context, false, groupId, bean.getMemberUid(), new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            list.remove(bean);
                            notifyDataSetChanged();
                            GroupInfoActivity.isDeleteMember = true;
                            UCommonUtil.getUtil().showToast(context, "移除成功");
                        }else {
                            UCommonUtil.getUtil().showToast(context, "移除失败");
                        }
                    }
                });
            }
        }, null);
    }
}


